#include "Lander.h"


Lander::Lander(void)
{
	starting_position = CL_Pointf(50,50);
	maxFuel = 700;
	maxThrust = 20.0f;
	reset();
}

Lander::Lander(int x, int y, float _maxThrust, int _maxFuel)
{
	starting_position = CL_Pointf(x,y);
	maxFuel = _maxFuel;
	maxThrust = _maxThrust;
	reset();
}

Lander::~Lander(void)
{
}

void Lander::update(int delta)
{
	CL_Pointf acceleration = gravity; //default acceleration
	float dt = delta/1000.0f;

	if(fuel > 0) {
		acceleration += thrust; //thrust is generated

		if(thrust.length() > 0)
			fuel = max(0,fuel-dt); //fuel is being used
	}
	
	velocity += acceleration*dt; 

	position += velocity*dt;
}


void Lander::setVerticalThrust(float f)
{
	thrust.y = -f;
}

void Lander::setHorizontalThrust(float f)
{
	thrust.x = f;
}


void Lander::draw(CL_GraphicContext& gc)
{
	//CL_Draw::box(gc, position-5.0f, position-5.0f, CL_Colorf::aliceblue);
	float size = 10.0f;

	CL_Pointf a = position - CL_Pointf(size,0);
	CL_Pointf b = position - CL_Pointf(0,size);
	CL_Pointf c = position + CL_Pointf(size,0);

	CL_Draw::triangle(gc, a,b,c,CL_Colorf::aliceblue);
}

void Lander::reset()
{
	fuel = maxFuel;
	position = starting_position;
	gravity = CL_Pointf(0,9.81);
	velocity = CL_Pointf(0,0);
}

float Lander::getMaxThrust()
{
	return maxThrust;
}

CL_Point Lander::getPosition()
{
	return position;
}

CL_Pointf Lander::getVelocity()
{
	return velocity;
}

float Lander::getFuelPercent()
{
	return (float) fuel/maxFuel;
}

int Lander::getFuel()
{
	return fuel;
}